package cn.sdhuijin.healthexam.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import cn.sdhuijin.healthexam.DatabaseHelper;
import cn.sdhuijin.healthexam.ExitApplication;
import cn.sdhuijin.healthexam.R;
import cn.sdhuijin.healthexam.bean.User;
import cn.sdhuijin.healthexam.bean.UserInfoBean;
import cn.sdhuijin.healthexam.control.AbstractControlActivity;
import cn.sdhuijin.healthexam.ui.popup.MenuItem;
import cn.sdhuijin.healthexam.ui.popup.PopupMenu;
import cn.sdhuijin.healthexam.ui.popup.PopupMenu.OnItemSelectedListener;
import cn.sdhuijin.healthexam.util.ConnectBluetooth;
import cn.sdhuijin.healthexam.util.Constants;
import cn.sdhuijin.healthexam.util.ReadWriteProperties;
import cn.sdhuijin.healthexam.util.Util;

/**
 * 负责蓝牙开启并连接身份证验证机器获取身份证信息
 * 
 * 修改完毕
 * 
 * @author houyuchun
 * 
 */
public class ScanIdentifyActivity extends AbstractControlActivity implements
		OnClickListener, OnItemSelectedListener {

	private static final int HEALTH_EXAM_FLAG = 0;
	private static final int JINGSHENBING_VIST_FLAG = 1;
	private static final int DOC_EDIT_FLAG = 2;
	private static final int GAOXUEYA_VISIT_FLAG = 3;
	private static final int TANGNIAOBING_VISIT_FLAG = 4;
	private static final int ZHONGYI_TIZHI_PANDING_FLAG = 5;
	private static final int CHANQIAN_FIRST_FLAG = 6;
	private static final int CHANQIAN_SECOND_FLAG = 7;
	private static final int CHANHOU_FLAG = 8;
	private static final int CHANHOU_SISHIER_FLAG = 9;
	private static final int XINSHENGER_FIRST_FLAG = 10;
	private static final int XINSHENGER_MANYUE_FLAG = 11;
	private static final int XINSHENGER_SHIERYUE_FLAG = 12;
	private static final int XINSHENGER_SANSUI_FLAG = 13;
	private static final int XINSHENGER_SANYUE_FLAG = 14;
	private static final int XINSHENGER_LIUYUE_FLAG = 15;
	private static final int XINSHENGER_BAYUE_FLAG = 16;
	private static final int XINSHENGER_SHIBAYUE_FLAG = 17;
	private static final int XINSHENGER_ERSHISIYUE_FLAG = 18;
	private static final int XINSHENGER_SANSHIYUE_FLAG = 19;
	private static final int XINSHENGER_SISUI_FLAG = 20;
	private static final int XINSHENGER_WUSUI_FLAG = 21;
	private static final int XINSHENGER_LIUSUI_FLAG = 22;
	private static final int CHANQIAN_THIRD_FLAG = 23;
	private static final int CHANQIAN_FORTH_FLAG = 24;
	private static final int CHANQIAN_FIFTH_FLAG = 25;

	// 身份证图片
	private ImageView ivUserPhoto;
	// 姓名编辑框
	private EditText etUserName;
	// 身份证号编辑框
	private EditText etIdentifyNum;
	// 性别编辑框
	private EditText etUserSex;
	// 出生日期编辑框
	private EditText etUserBirth;
	// 发证机关编辑框
	private EditText etFromDep;
	// 证件有效期编辑框
	private EditText etExpiredDate;
	// 籍贯编辑框
	private EditText etUserHome;
	// 民族编辑框
	private EditText etUserNation;
	// 获取蓝牙连接，打开身份证扫描仪
	private ConnectBluetooth conn = new ConnectBluetooth();
	private ReadWriteProperties readWriteProperties;
	// 数据库连接
	private DatabaseHelper databaseHelper;
	// 身份证号
	private String identifyNum = "";
	private UserInfoBean userInfoBean;
	// 页面
	private String pageFrom = "";
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 去掉标题
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_scan_identify);
		ExitApplication.getInstance().addActivity(this);

		// 数据库连接
		databaseHelper = new DatabaseHelper(this);
		readWriteProperties = new ReadWriteProperties();
		Intent intent = getIntent();
		if (intent != null) {
			pageFrom = intent.getStringExtra("pageFrom");
		}
		// 姓名编辑框
		etUserName = initEditTextView(R.id.edt_user_name);
		// 身份证号编辑框
		etIdentifyNum = initEditTextView(R.id.edt_identify_num);
		// 性别编辑框
		etUserSex = initEditTextView(R.id.edt_user_sex);
		// 出生日期编辑框
		etUserBirth = initEditTextView(R.id.edt_user_birth);
		// 民族编辑框
		etUserNation = initEditTextView(R.id.edt_user_nation);
		// 发证机关编辑框
		etFromDep = initEditTextView(R.id.edt_from_dep);
		// 籍贯编辑框
		etUserHome = initEditTextView(R.id.edt_user_home);
		// 证件有效期编辑框
		etExpiredDate = initEditTextView(R.id.edt_expiry_date);

		// 照片
		ivUserPhoto = (ImageView) findViewById(R.id.img_user_photo);

		addTextView(R.id.edt_user_name, "姓名");
		addTextView(R.id.edt_identify_num, "身份证号");
		addTextView(R.id.edt_user_sex, "性别");
		addTextView(R.id.edt_user_birth, "出生日期");
		addTextView(R.id.edt_user_nation, "民族");
		addTextView(R.id.edt_from_dep, "发证机关");
		addTextView(R.id.edt_user_home, "籍贯");
		addTextView(R.id.edt_expiry_date, "证件有效期");

		// Properties文件路径
		controlManager.setPropertiesPath(Constants.PROPERTIES_PATH
				+ "scanidentify.txt");
		controlManager.toControl();

		this.setEditTextListener(R.id.edt_user_birth);

		this.setButtonListener(this, R.id.btn_scan_identify, R.id.btn_back,
				R.id.btn_shougong);
		// test begin
		progressDialog = new ProgressDialog(this);
		progressDialog.setTitle("提示");
		progressDialog.setMessage("正在连接蓝牙设备，请稍等...");
		progressDialog.setCancelable(false);
		// test end
	}

	@Override
	protected void onStart() {
		super.onStart();
		if (!User.bluetoothConn) {
			new Thread(new MyThread()).start();
			progressDialog.show();
		}
	}

	/**
	 * 按钮点击处理事件
	 */
	@Override
	public void onClick(View arg0) {
		// 获取点击按钮ID
		int id = (Integer) arg0.getId();
		switch (id) {
		case R.id.btn_scan_identify:
			Log.i(ScanIdentifyActivity.class.getSimpleName(), "连接身份证扫描器结果： "
					+ User.bluetoothConn);
			// 连接失败
			if (!User.bluetoothConn) {
				// 提示连接身份证扫描器失败
				this.toastInformation(R.string.toast_bluetooth_conn_error);
			} else {
				userInfoBean = User.conn.readIdentifyInfo();
				// 获取身份信息为空
				if (userInfoBean == null) {
					// 提示身份证信息获取为空
					this.toastInformation(R.string.toast_scan_info_error);
				} else {
					identifyNum = userInfoBean.getIdenfyCardNum();
					// 将扫描身份证信息赋值给对应编辑框
					setValueToEditText(userInfoBean);
					controlManager.toProperties();
					// 检索身份证表，如果不存在当前扫描人员，插入身份证信息到身份证表
					databaseHelper.insertInfoIfNotExist(userInfoBean);
					// 检查该人员是否已经存在档案
					boolean exist = databaseHelper.checkUserExistDoc(
							userInfoBean.getIdenfyCardNum(), "档案");
					// 如果当前扫描人员存在档案信息
					if (exist) {
						// 获取档案创建成功标志和创建日期
						Properties properties = databaseHelper
								.getUserDocCompleteFlag(
										userInfoBean.getIdenfyCardNum(), "档案");
						// 创建成功标志
						if (databaseHelper.COMPLETE_FLAG_OK.equals(properties
								.get("completeFlag"))) {
							// 跳转对应页面或者弹出选择菜单
							startPageOrShowPopMenuItem(pageFrom);
						} else if (databaseHelper.COMPLETE_FLAG_NO
								.equals(properties.get("completeFlag"))) {
							String createDate = properties.get("createDate")
									.toString();
							startActivity(createDate, DOC_EDIT_FLAG);
						}
					} else {
						// 系统时间
						String currentTime = Util.getCurrentDateTime();
						// Properties文件存储
						readWriteProperties.storeProperties(currentTime,
								userInfoBean);
						// 保存新建档案记录到索引表
						savaInfoToIndex(currentTime, identifyNum, "档案");
						startActivity(currentTime, DOC_EDIT_FLAG);
					}
				}
			}
			break;
		case R.id.btn_shougong:
			// 保存之前合法性验证，获取非法内容ID
			List<Integer> list = getInvalidateContentIds();
			// 如果存在不合法内容
			if (list.size() > 0) {
				// 非法内容背景颜色提示
				changeInvalidateContentBackColor(list);
				this.toastInformation(R.string.toast_input_error);
			} else {
				if (Util.allIsNotEmpty(getControlValue(R.id.edt_identify_num),
						getControlValue(R.id.edt_user_name))) {
					userInfoBean = new UserInfoBean();
					// 姓名
					userInfoBean
							.setUserName(getControlValue(R.id.edt_user_name));
					// 性别
					userInfoBean.setUserSex(getControlValue(R.id.edt_user_sex));
					// 民族
					userInfoBean
							.setUserNation(getControlValue(R.id.edt_user_nation));
					// 出生日期
					userInfoBean
							.setUserBirth(getControlValue(R.id.edt_user_birth));
					// 地址
					userInfoBean
							.setUserAddress(getControlValue(R.id.edt_user_home));
					// 身份证号
					userInfoBean
							.setIdenfyCardNum(getControlValue(R.id.edt_identify_num));
					// 签发机关
					userInfoBean
							.setFromDepartment(getControlValue(R.id.edt_from_dep));
					// 有限期限
					userInfoBean
							.setExpiryDate(getControlValue(R.id.edt_expiry_date));
					identifyNum = userInfoBean.getIdenfyCardNum();
					controlManager.toProperties();
					// 检索身份证表，如果不存在当前扫描人员，插入身份证信息到身份证表
					databaseHelper.insertInfoIfNotExist(userInfoBean);
					// 检查该人员是否已经存在档案
					boolean exist = databaseHelper.checkUserExistDoc(
							userInfoBean.getIdenfyCardNum(), "档案");
					// 如果当前扫描人员存在档案信息
					if (exist) {
						// 获取档案创建成功标志和创建日期
						Properties properties = databaseHelper
								.getUserDocCompleteFlag(
										userInfoBean.getIdenfyCardNum(), "档案");
						// 创建成功标志
						if (databaseHelper.COMPLETE_FLAG_OK.equals(properties
								.get("completeFlag"))) {
							// 跳转对应页面或者弹出选择菜单
							startPageOrShowPopMenuItem(pageFrom);
						} else if (databaseHelper.COMPLETE_FLAG_NO
								.equals(properties.get("completeFlag"))) {
							String createDate = properties.get("createDate")
									.toString();
							startActivity(createDate, DOC_EDIT_FLAG);
						}
					} else {
						// 系统时间
						String currentTime = Util.getCurrentDateTime();
						// Properties文件存储
						readWriteProperties.storeProperties(currentTime,
								userInfoBean);
						// 保存新建档案记录到索引表
						savaInfoToIndex(currentTime, identifyNum, "档案");
						startActivity(currentTime, DOC_EDIT_FLAG);
					}
				} else {
					// 提示身份证信息获取为空
					this.toastInformation(R.string.toast_identitify_not_null_error);
				}
			}
			break;
		case R.id.btn_back:
			this.finish();
			break;
		default:
			break;
		}
	}

	private void showPopMenuItem() {
		PopupMenu menu = new PopupMenu(this);
		menu.setHeaderTitle("菜单选项");
		menu.setOnItemSelectedListener(this);
		menu.add(HEALTH_EXAM_FLAG, R.string.item_health_exam).setIcon(
				getResources().getDrawable(R.drawable.health_exam));
		menu.add(CHANQIAN_FIRST_FLAG, R.string.chanqian_first).setIcon(
				getResources().getDrawable(R.drawable.yunchanfu));
		menu.add(CHANQIAN_SECOND_FLAG, R.string.chanqian_second).setIcon(
				getResources().getDrawable(R.drawable.yunchanfu));
		menu.add(CHANQIAN_THIRD_FLAG, R.string.chanqian_third).setIcon(
				getResources().getDrawable(R.drawable.yunchanfu));
		menu.add(CHANQIAN_FORTH_FLAG, R.string.chanqian_forth).setIcon(
				getResources().getDrawable(R.drawable.yunchanfu));
		menu.add(CHANQIAN_FIFTH_FLAG, R.string.chanqian_fifth).setIcon(
				getResources().getDrawable(R.drawable.yunchanfu));
		menu.add(CHANHOU_FLAG, R.string.chanhou).setIcon(
				getResources().getDrawable(R.drawable.yunchanfu));
		menu.add(CHANHOU_SISHIER_FLAG, R.string.chanhou_sishier).setIcon(
				getResources().getDrawable(R.drawable.yunchanfu));
		menu.add(XINSHENGER_FIRST_FLAG, R.string.new_chiled).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(XINSHENGER_MANYUE_FLAG, R.string.new_chiled_manyue).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(XINSHENGER_SANYUE_FLAG, R.string.new_chiled_sanyue).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(XINSHENGER_LIUYUE_FLAG, R.string.new_chiled_liuyue).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(XINSHENGER_BAYUE_FLAG, R.string.new_chiled_bayue).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(XINSHENGER_SHIERYUE_FLAG, R.string.new_chiled_shieryue)
				.setIcon(getResources().getDrawable(R.drawable.xinshenger));
		menu.add(XINSHENGER_SHIBAYUE_FLAG, R.string.new_chiled_shibayue)
				.setIcon(getResources().getDrawable(R.drawable.xinshenger));
		menu.add(XINSHENGER_ERSHISIYUE_FLAG, R.string.new_chiled_ershisiyue)
				.setIcon(getResources().getDrawable(R.drawable.xinshenger));
		menu.add(XINSHENGER_SANSHIYUE_FLAG, R.string.new_chiled_sanshiyue)
				.setIcon(getResources().getDrawable(R.drawable.xinshenger));
		menu.add(XINSHENGER_SANSUI_FLAG, R.string.new_chiled_sansui).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(XINSHENGER_SISUI_FLAG, R.string.new_chiled_sisui).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(XINSHENGER_WUSUI_FLAG, R.string.new_chiled_wusui).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(XINSHENGER_LIUSUI_FLAG, R.string.new_chiled_liusui).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(JINGSHENBING_VIST_FLAG, R.string.item_jingshenbing_visit)
				.setIcon(getResources().getDrawable(R.drawable.jingshenbing));
		menu.add(GAOXUEYA_VISIT_FLAG, R.string.item_gaoxueya_visit).setIcon(
				getResources().getDrawable(R.drawable.gaoxueya));
		menu.add(TANGNIAOBING_VISIT_FLAG, R.string.item_tangniaobing_visit)
				.setIcon(getResources().getDrawable(R.drawable.tangniaobing));
		menu.show();
	}

	/**
	 * 将扫描身份证信息赋值给对应编辑框
	 * 
	 * @param userInfoBean
	 *            身份证信息
	 */
	private void setValueToEditText(UserInfoBean userInfoBean) {
		// 姓名
		if (userInfoBean.getUserName() != null) {
			etUserName.setText(userInfoBean.getUserName());
		}
		// 身份证号
		if (userInfoBean.getIdenfyCardNum() != null) {
			etIdentifyNum.setText(userInfoBean.getIdenfyCardNum());
		}
		// 性别
		if (userInfoBean.getUserSex() != null) {
			etUserSex.setText(userInfoBean.getUserSex());
		}
		// 民族
		if (userInfoBean.getUserNation() != null) {
			etUserNation.setText(userInfoBean.getUserNation());
		}
		// 出生日期
		if (userInfoBean.getUserBirth() != null) {
			etUserBirth.setText(userInfoBean.getUserBirth());
		}
		// 住址
		if (userInfoBean.getUserAddress() != null) {
			etUserHome.setText(userInfoBean.getUserAddress());
		}
		// 签发机关
		if (userInfoBean.getFromDepartment() != null) {
			etFromDep.setText(userInfoBean.getFromDepartment());
		}
		// 证件有效期
		if (userInfoBean.getExpiryDate() != null) {
			etExpiredDate.setText(userInfoBean.getExpiryDate());
		}
		// 照片
		if (userInfoBean.getUserPhoto() != null) {
			ivUserPhoto.setImageBitmap(userInfoBean.getUserPhoto());
		}
	}

	/**
	 * 
	 * @param createDate
	 * @param flag
	 */
	private void startActivity(String createDate, int flag) {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		// 建档日期
		bundle.putString("createDate", createDate);
		intent.putExtra("bundle", bundle);
		switch (flag) {
		case HEALTH_EXAM_FLAG:
			// 跳转健康体检页面
			intent.setClass(this, HealthExamFirstActivity.class);
			startActivity(intent);
			break;
		case JINGSHENBING_VIST_FLAG:
			// 跳转精神病随访
			intent.setClass(this, EditVisitMentalFirstActivity.class);
			startActivity(intent);
			break;
		case DOC_EDIT_FLAG:
			// 跳转档案信息编辑页面
			intent.setClass(this, EditPersonDocFirstActivity.class);
			startActivity(intent);
			break;
		case GAOXUEYA_VISIT_FLAG:
			// 高血压随访
			intent.setClass(this, EditVisitHypertensionFirstActivity.class);
			startActivity(intent);
			break;
		case TANGNIAOBING_VISIT_FLAG:
			// 糖尿病随访
			intent.setClass(this, EditVisitDiabetesFirstActivity.class);
			startActivity(intent);
			break;
		case ZHONGYI_TIZHI_PANDING_FLAG:
			// 中医体质判定
			intent.setClass(this, BodyJudgeFirstActivity.class);
			startActivity(intent);
			break;
		case CHANQIAN_FIRST_FLAG:
			// 产前第一次随访
			intent.setClass(this, ChanQianVisitFirstActivity.class);
			startActivity(intent);
			break;
		case CHANQIAN_SECOND_FLAG:
			// 产前第二次随访
			intent.setClass(this, ChanQianSecondVisitFirstActivity.class);
			startActivity(intent);
			break;
		case CHANQIAN_THIRD_FLAG:
			// 产前第三次随访
			intent.setClass(this, ChanQianThirdVisitFirstActivity.class);
			startActivity(intent);
			break;
		case CHANQIAN_FORTH_FLAG:
			// 产前第四次随访
			intent.setClass(this, ChanQianForthVisitFirstActivity.class);
			startActivity(intent);
			break;
		case CHANQIAN_FIFTH_FLAG:
			// 产前第五次随访
			intent.setClass(this, ChanQianFifthVisitFirstActivity.class);
			startActivity(intent);
			break;
		case CHANHOU_FLAG:
			// 产后随访
			intent.setClass(this, ChanHouVisitActivity.class);
			startActivity(intent);
			break;
		case CHANHOU_SISHIER_FLAG:
			// 产后四十二天随访
			intent.setClass(this, ChanHouSiShiErVisitActivity.class);
			startActivity(intent);
			break;
		case XINSHENGER_FIRST_FLAG:
			// 新生儿随访
			intent.setClass(this, NewChildrenVisitFirstActivity.class);
			startActivity(intent);
			break;
		case XINSHENGER_MANYUE_FLAG:
			// 新生儿满月随访
			intent.setClass(this, ManYueVisitFirstActivity.class);
			startActivity(intent);
			break;
		case XINSHENGER_SANYUE_FLAG:
			// 新生儿三月龄随访
			intent.setClass(this, SanYueVisitFirstActivity.class);
			startActivity(intent);
			break;
		case XINSHENGER_LIUYUE_FLAG:
			// 新生儿六月龄随访
			intent.setClass(this, LiuYueVisitFirstActivity.class);
			startActivity(intent);
			break;
		case XINSHENGER_BAYUE_FLAG:
			// 新生儿八月龄随访
			intent.setClass(this, BaYueVisitFirstActivity.class);
			startActivity(intent);
			break;
		case XINSHENGER_SHIBAYUE_FLAG:
			// 新生儿十八月龄随访
			intent.setClass(this, ShiBaYueVisitFirstActivity.class);
			startActivity(intent);
			break;
		case XINSHENGER_SHIERYUE_FLAG:
			// 新生儿十二月龄随访
			intent.setClass(this, ShiErYueVisitFirstActivity.class);
			startActivity(intent);
			break;
		case XINSHENGER_ERSHISIYUE_FLAG:
			// 新生儿二十四月龄随访
			intent.setClass(this, ErShiSiYueVisitFirstActivity.class);
			startActivity(intent);
			break;
		case XINSHENGER_SANSHIYUE_FLAG:
			// 新生儿三十月龄随访
			intent.setClass(this, SanShiYueVisitFirstActivity.class);
			startActivity(intent);
			break;
		case XINSHENGER_SANSUI_FLAG:
			// 新生儿三岁龄随访
			intent.setClass(this, SanSuiVisitFirstActivity.class);
			startActivity(intent);
			break;
		case XINSHENGER_SISUI_FLAG:
			// 新生儿四岁龄随访
			intent.setClass(this, SiSuiVisitFirstActivity.class);
			startActivity(intent);
			break;
		case XINSHENGER_WUSUI_FLAG:
			// 新生儿五岁龄随访
			intent.setClass(this, WuSuiVisitFirstActivity.class);
			startActivity(intent);
			break;
		case XINSHENGER_LIUSUI_FLAG:
			// 新生儿六岁龄随访
			intent.setClass(this, LiuSuiVisitFirstActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	@Override
	public void onItemSelected(MenuItem item) {
		// 系统时间
		String currentTime = Util.getCurrentDateTime();
		switch (item.getItemId()) {
		case HEALTH_EXAM_FLAG:
			// Properties文件存储
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "体检");
			startActivity(currentTime, HEALTH_EXAM_FLAG);
			break;
		case JINGSHENBING_VIST_FLAG:
			// Properties文件存储
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "精神病随访");
			startActivity(currentTime, JINGSHENBING_VIST_FLAG);
			break;
		case GAOXUEYA_VISIT_FLAG:
			// Properties文件存储
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "高血压随访");
			startActivity(currentTime, GAOXUEYA_VISIT_FLAG);
			break;
		case XINSHENGER_FIRST_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿随访");
			startActivity(currentTime, XINSHENGER_FIRST_FLAG);
			break;
		case XINSHENGER_MANYUE_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿满月随访");
			startActivity(currentTime, XINSHENGER_MANYUE_FLAG);
			break;
		case XINSHENGER_SANYUE_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿三月龄随访");
			startActivity(currentTime, XINSHENGER_SANYUE_FLAG);
			break;
		case XINSHENGER_LIUYUE_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿六月龄随访");
			startActivity(currentTime, XINSHENGER_LIUYUE_FLAG);
			break;
		case XINSHENGER_BAYUE_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿八月龄随访");
			startActivity(currentTime, XINSHENGER_BAYUE_FLAG);
			break;
		case XINSHENGER_SHIERYUE_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿十二月龄随访");
			startActivity(currentTime, XINSHENGER_SHIERYUE_FLAG);
			break;
		case XINSHENGER_SHIBAYUE_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿十八月龄随访");
			startActivity(currentTime, XINSHENGER_SHIBAYUE_FLAG);
			break;
		case XINSHENGER_ERSHISIYUE_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿二十四月龄随访");
			startActivity(currentTime, XINSHENGER_ERSHISIYUE_FLAG);
			break;
		case XINSHENGER_SANSHIYUE_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿三十月龄随访");
			startActivity(currentTime, XINSHENGER_SANSHIYUE_FLAG);
			break;
		case XINSHENGER_SANSUI_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿三岁龄随访");
			startActivity(currentTime, XINSHENGER_SANSUI_FLAG);
			break;
		case XINSHENGER_SISUI_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿四岁龄随访");
			startActivity(currentTime, XINSHENGER_SISUI_FLAG);
			break;
		case XINSHENGER_WUSUI_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿五岁龄随访");
			startActivity(currentTime, XINSHENGER_WUSUI_FLAG);
			break;
		case XINSHENGER_LIUSUI_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿六岁龄随访");
			startActivity(currentTime, XINSHENGER_LIUSUI_FLAG);
			break;
		case CHANQIAN_FIRST_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "产前第一次随访");
			startActivity(currentTime, CHANQIAN_FIRST_FLAG);
			break;
		case CHANQIAN_SECOND_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "产前第二次随访");
			startActivity(currentTime, CHANQIAN_SECOND_FLAG);
			break;
		case CHANQIAN_THIRD_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "产前第三次随访");
			startActivity(currentTime, CHANQIAN_THIRD_FLAG);
			break;
		case CHANQIAN_FORTH_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "产前第四次随访");
			startActivity(currentTime, CHANQIAN_FORTH_FLAG);
			break;
		case CHANQIAN_FIFTH_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "产前第五次随访");
			startActivity(currentTime, CHANQIAN_FIFTH_FLAG);
			break;
		case CHANHOU_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "产后随访");
			startActivity(currentTime, CHANHOU_FLAG);
			break;
		case CHANHOU_SISHIER_FLAG:
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "产后四十二天随访");
			startActivity(currentTime, CHANHOU_SISHIER_FLAG);
			break;
		default:
			break;
		}
	}

	/**
	 * 根据来自页面标示判断是跳转指定页面或是弹出菜单选项
	 * 
	 * @param pageFrom
	 */
	private void startPageOrShowPopMenuItem(String pageFrom) {
		// 系统时间
		String currentTime = Util.getCurrentDateTime();
		if ("扫描".equals(pageFrom) || "档案".equals(pageFrom)) {
			showPopMenuItem();
		} else if ("体检".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "体检");
			startActivity(currentTime, HEALTH_EXAM_FLAG);
		} else if ("精神病随访".equals(pageFrom)) {
			// Properties文件存储
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "精神病随访");
			startActivity(currentTime, JINGSHENBING_VIST_FLAG);
		} else if ("高血压随访".equals(pageFrom)) {
			// Properties文件存储
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "高血压随访");
			startActivity(currentTime, GAOXUEYA_VISIT_FLAG);
		} else if ("糖尿病随访".equals(pageFrom)) {
			// Properties文件存储
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "糖尿病随访");
			startActivity(currentTime, TANGNIAOBING_VISIT_FLAG);
		} else if ("中医体质判定".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "中医体质判定");
			startActivity(currentTime, ZHONGYI_TIZHI_PANDING_FLAG);
		} else if ("产前第一次随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "产前第一次随访");
			startActivity(currentTime, CHANQIAN_FIRST_FLAG);
		} else if ("产前第二次随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "产前第二次随访");
			startActivity(currentTime, CHANQIAN_SECOND_FLAG);
		} else if ("产前第三次随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "产前第三次随访");
			startActivity(currentTime, CHANQIAN_THIRD_FLAG);
		} else if ("产前第四次随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "产前第四次随访");
			startActivity(currentTime, CHANQIAN_FORTH_FLAG);
		} else if ("产前第五次随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "产前第五次随访");
			startActivity(currentTime, CHANQIAN_FIFTH_FLAG);
		} else if ("产后随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "产后随访");
			startActivity(currentTime, CHANHOU_FLAG);
		} else if ("产后四十二天随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "产后四十二天随访");
			startActivity(currentTime, CHANHOU_SISHIER_FLAG);
		} else if ("新生儿随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿随访");
			startActivity(currentTime, XINSHENGER_FIRST_FLAG);
		} else if ("新生儿满月随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿满月随访");
			startActivity(currentTime, XINSHENGER_MANYUE_FLAG);
		} else if ("新生儿三月龄随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿三月龄随访");
			startActivity(currentTime, XINSHENGER_SANYUE_FLAG);
		} else if ("新生儿六月龄随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿六月龄随访");
			startActivity(currentTime, XINSHENGER_LIUYUE_FLAG);
		} else if ("新生儿八月龄随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿八月龄随访");
			startActivity(currentTime, XINSHENGER_BAYUE_FLAG);
		} else if ("新生儿十二月龄随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿十二月龄随访");
			startActivity(currentTime, XINSHENGER_SHIERYUE_FLAG);
		} else if ("新生儿十八月龄随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿十八月龄随访");
			startActivity(currentTime, XINSHENGER_SHIBAYUE_FLAG);
		} else if ("新生儿二十四月龄随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿二十四月龄随访");
			startActivity(currentTime, XINSHENGER_ERSHISIYUE_FLAG);
		} else if ("新生儿三十月龄随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿三十月龄随访");
			startActivity(currentTime, XINSHENGER_SANSHIYUE_FLAG);
		} else if ("新生儿三岁龄随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿三岁龄随访");
			startActivity(currentTime, XINSHENGER_SANSUI_FLAG);
		} else if ("新生儿四岁龄随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿四岁龄随访");
			startActivity(currentTime, XINSHENGER_SISUI_FLAG);
		} else if ("新生儿五岁龄随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿五岁龄随访");
			startActivity(currentTime, XINSHENGER_WUSUI_FLAG);
		} else if ("新生儿六岁龄随访".equals(pageFrom)) {
			readWriteProperties.storeProperties(currentTime, userInfoBean);
			savaInfoToIndex(currentTime, identifyNum, "新生儿六岁龄随访");
			startActivity(currentTime, XINSHENGER_LIUSUI_FLAG);
		}
	}

	// test begin
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				progressDialog.dismiss();
				String status = (String) msg.obj;
				if ("连接成功".equals(status)) {
					ScanIdentifyActivity.this
							.toastInformation(R.string.toast_bluetooth_conn_ok);
					User.bluetoothConn = true;
					User.conn = conn;
				} else {
					// 提示连接身份证扫描器失败
					ScanIdentifyActivity.this
							.toastInformation(R.string.toast_bluetooth_conn_error);
				}
				break;

			default:
				break;
			}
		}
	};

	public class MyThread implements Runnable {
		@Override
		public void run() {
			try {
				boolean connect = conn.getBluetoothConnect();
				Message msg = Message.obtain();
				if (connect) {
					msg.obj = "连接成功";
				} else {
					msg.obj = "连接失败";
				}
				msg.what = 1;
				handler.sendMessage(msg);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private List<Integer> getInvalidateContentIds() {
		List<Integer> list = null;
		try {
			list = new ArrayList<Integer>();
			String validateStr = "";
			// 姓名
			validateStr = ((EditText) this.findViewById(R.id.edt_user_name))
					.getText().toString().trim();
			if ("".equals(validateStr)) {
				list.add(R.id.edt_user_name);
			}
			// 身份证号
			validateStr = ((EditText) this.findViewById(R.id.edt_identify_num))
					.getText().toString().trim();
			if ("".equals(validateStr) || !Util.IDCardValidate(validateStr)) {
				list.add(R.id.edt_identify_num);
			}
			// 性别
			validateStr = ((EditText) this.findViewById(R.id.edt_user_sex))
					.getText().toString().trim();
			if (!"".equals(validateStr)
					&& (!"男".equals(validateStr) && !"女".equals(validateStr))) {
				list.add(R.id.edt_user_sex);
			}
			// 出生日期
			validateStr = ((EditText) this.findViewById(R.id.edt_user_birth))
					.getText().toString().trim();
			if (!"".equals(validateStr) && !Util.isDate(validateStr)) {
				list.add(R.id.edt_user_birth);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 改变录入信息不合法的控件背景颜色
	 * 
	 * @param list
	 *            控件ID列表
	 */
	private void changeInvalidateContentBackColor(List<Integer> list) {

		for (int i = 0; i < list.size(); i++) {
			if (R.id.edt_identify_num == list.get(i)) {
				((EditText) this.findViewById(R.id.edt_identify_num))
						.setBackgroundResource(R.drawable.diancha_test_pre);
			} else if (R.id.edt_user_birth == list.get(i)) {
				((EditText) this.findViewById(R.id.edt_user_birth))
						.setBackgroundResource(R.drawable.diancha_test_pre);
			} else if (R.id.edt_user_name == list.get(i)) {
				((EditText) this.findViewById(R.id.edt_user_name))
						.setBackgroundResource(R.drawable.diancha_test_pre);
			} else if (R.id.edt_user_sex == list.get(i)) {
				((EditText) this.findViewById(R.id.edt_user_sex))
						.setBackgroundResource(R.drawable.diancha_test_pre);
			}
		}
	}

}
